<?php
//防止非法访问
defined('ACC') || exit('ACC Denied');


class mysql extends db{
     private static $ins = NULL;
     private $conn = NULL;
     private $conf = array();
     
      function __construct(){
          $this->conf = conf::getIns();
          // print_r($this->conf);
          $this->connect($this->conf->host,$this->conf->user,$this->conf->pwd);
          $this->select_db($this->conf->db);          
          $this->setChar($this->conf->char);
     }
   
     public static function getIns(){
          if(!(self::$ins instanceof self)){
               self::$ins = new self();
          }
          return self::$ins;
     }

   public function connect($h,$u,$p){
        // $this->conn = mysql_connect($h,$u,$p);    mysql_connect() 在php7.0中已废弃
          $this->conn = mysqli_connect($h,$u,$p);
        if(!$this->conn){
              $err = new Exception('连接失败');
              throw $err;
        }
   }

   protected function select_db($db){
          $sql = 'use ' .  $db;
          $this ->query($sql);    
   }

   protected function setChar($char){
    $sql = 'set names ' . $char;
      $this->query($sql);
   }

   public function query($sql){
       //发送sql语句
         // $rs = mysql_query($sql,$this->conn);    //发送sql语句 ,PHP7.0中已废弃
          $rs = mysqli_query($this->conn,$sql);
       //将sql记录到日志
          log::write($sql);
          return $rs;
   }

 /**
      $table 表名
      $arr 数据
      $mode 方式 （更新or插入）
      $where 更新或插入的条件
 **/
  public function autoExecute($table,$arr, $mode='insert',$where = 'where 1 limit 1'){
        /**
            insert into tbname (username,passwd,email) walues('',)
            把所有的键名用“ . '' 接起来
            implode(',' , array_keys($arr));
            implode(" '  , ' ",array_values($arr));
         **/
//             echo '后台';
            if(!is_array($arr)){
                  return false;
            }

            if($mode == 'update'){      //更新数据
                  $sql = 'update ' . $table . ' set ';
                  foreach ($arr as $k => $v) {
                     $sql .= $k . "='" . $v . "', " ;
                  }
//                  print_r($sql);  echo "<br />";
                  // $sql = rtrim($sql,",");
                  $sql = substr($sql,0,-2);
                  $sql .= $where;
                  // print_r($sql);  exit();
                  return $this->query($sql);
            }

            // if($mode == 'update') {
            //      $sql = 'update ' . $table .' set ';
            //      foreach($arr as $k=>$v) {
            //          $sql .= $k . "='" . $v ."',";
            //     }
            //      $sql = rtrim($sql,',');
            //     $sql .= $where;
            //    return $this->query($sql);
            //  }

            //添加数据
            $sql = 'insert into ' . $table . '(' . implode(',' , array_keys($arr)) . ')';
            $sql .= ' values( \'' ;
            $sql .= implode("','" , array_values($arr));
            $sql .= '\')';
              // var_dump(implode("','",array_keys($arr))); 
            return  $this->query($sql);
            // if($mode == 'insert'){     //添加数据
            //     $sql = 'insert into ' . $table."(t1,t2)"  .  ' values(' . ' "' .$arr['t1'] . ' "," ' . $arr['t2'] . '" ' . ')';
            //     return $this->query($sql);
            // }
  }
  

  public function getAll($sql){
       $rs = $this->query($sql);
        $list = array();
        while($row = mysqli_fetch_assoc($rs)) {
            $list[] = $row;
        }
        return $list;
  }

   public function getRow($sql){
       $rs = $this->query($sql);
       return mysqli_fetch_assoc($rs);
  }

    public function getOne($sql){
      $rs = $this->query($sql);
      $row = mysqli_fetch_row($rs);
      return $row[0];
  }


   // 返回影响行数的函数
    public function affected_rows() {
        return mysqli_affected_rows($this->conn);
    }

    // 返回最新的auto_increment列的自增长的值
    public function insert_id() {
        return mysqli_insert_id($this->conn);
    }



}  









?>